﻿using Caliburn.Micro;
using SS.WPF.Framework.Localization;

namespace SS.WPF.Framework.ValidationRule
{
    using System.Globalization;
    using System.Windows.Controls;
    using System.Linq;
    using System.Collections.Generic;
    using System;
    using System.Text.RegularExpressions;

    public class IPAddressValidValidationRule : CommonValidationRule
    {
        private bool IsIPAddress(string ipAddress)
        {
            const string ipValidationPartent = @"^([1-9]?\d|1\d\d|2[0-4]\d|25[0-5])\.([1-9]?\d|1\d\d|2[0-4]\d|25[0-5])\.([1-9]?\d|1\d\d|2[0-4]\d|25[0-5])\.([1-9]?\d|1\d\d|2[0-4]\d|25[0-5])$";
            IList<string> lIP = ipAddress.Split('.').ToList();
            string newAddress = string.Empty;
            if (!Regex.IsMatch(ipAddress, ipValidationPartent))
            {
                if (lIP.Count == 4)
                {
                    int equal255 = 0;
                    foreach (var item in lIP)
                    {
                        if (item.Length > 3)
                        {
                            return false;
                        }

                        var ip = item.TrimStart('0');
                        if (item.Length - ip.Length > 1)
                        {
                            return false;
                        }

                        if ("255".Equals(ip, StringComparison.OrdinalIgnoreCase))
                        {
                            equal255++;
                        }

                        newAddress += ip + ".";
                    }

                    if (String.IsNullOrEmpty(lIP[0].TrimStart('0')))
                    {
                        return false;
                    }

                    if (equal255 > 3)
                    {
                        return false;
                    }
                }
                else
                {
                    return false;
                }
            }

            if (string.IsNullOrEmpty(lIP[0].TrimStart('0')))
            {
                return false;
            }

            var check255 = lIP.Count(item => string.Compare("255", item, StringComparison.Ordinal) == 0);

            if (check255 == 4)
            {
                return false;
            }

            if (!string.IsNullOrEmpty(newAddress))
            {
                newAddress = newAddress.TrimEnd('.');
                if (!Regex.IsMatch(newAddress, ipValidationPartent))
                {
                    return false;
                }
            }

            return true;

        }

        public override ValidationResult Validate(object value, CultureInfo cultureInfo)
        {
            if (!IsEnabled)
            {
                return ValidationResult.ValidResult;
            }

            var ipAddress = value as string;
            if (ipAddress == null)
            {
                return new ValidationResult(false, IoC.Get<TranslationManager>().Translate("IPAddress.Constraint"));
            }

            var result = this.IsIPAddress(ipAddress);

            return new ValidationResult(result, IoC.Get<TranslationManager>().Translate("IPAddress.Constraint"));
        }
    }
}
